package cn.xiqi.cms.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.xiqi.cms.hibernate.beans.Loginfo;
import cn.xiqi.cms.oper.LogOper;
import net.sf.json.JSON;
import net.sf.json.JSONObject;

public class LogAction extends BaseAction {

	private static final long serialVersionUID = 7769153144248407020L;
	private Map<String, Object> map;
	private JSON json;
	public Integer page;
	public Integer size;
	public Integer usrid;
	public Integer type;
	public String sstart;
	public String send;
	
	public String getAll(){
		LogOper oper = new LogOper();
		List<Loginfo> infos;

		map = new HashMap<>();		
		try{
			infos = oper.getAll(page, size);
			map.put("success", true);
			map.put("total", infos.size());
			map.put("rows", infos);
			map.put("page", page);
			map.put("size", size);
		}catch(Exception e){
			map.put("success", false);
			map.put("mag", "读取日志失败");
		}finally{
			json = JSONObject.fromObject(map);
		}
		return SUCCESS;
	}
	public String getByUsr(){
		LogOper oper = new LogOper();
		List<Loginfo> infos;

		map = new HashMap<>();		
		try{
			infos = oper.getByUsr(usrid, page, size);
			map.put("success", true);
			map.put("total", infos.size());
			map.put("rows", infos);
			map.put("page", page);
			map.put("size", size);
		}catch(Exception e){
			map.put("success", false);
			map.put("mag", "读取日志失败");
		}finally{
			json = JSONObject.fromObject(map);
		}
		return SUCCESS;
	}
	public String getByType(){
		LogOper oper = new LogOper();
		List<Loginfo> infos;

		map = new HashMap<>();		
		try{
			infos = oper.getByType(type, page, size);
			map.put("success", true);
			map.put("total", infos.size());
			map.put("rows", infos);
			map.put("page", page);
			map.put("size", size);
		}catch(Exception e){
			map.put("success", false);
			map.put("mag", "读取日志失败");
		}finally{
			json = JSONObject.fromObject(map);
		}
		return SUCCESS;
	}
	public String getByTime(){
		LogOper oper = new LogOper();
		List<Loginfo> infos;
		SimpleDateFormat sdf = new SimpleDateFormat(TIMEFOEMAT);
		Date start;
		Date end;
		
		map = new HashMap<>();		
		try{
			start = sdf.parse(sstart);
			end = sdf.parse(send);
			infos = oper.getByTime(start, end, page, size);
			map.put("success", true);
			map.put("total", infos.size());
			map.put("rows", infos);
			map.put("page", page);
			map.put("size", size);
		}catch(Exception e){
			map.put("success", false);
			map.put("mag", "读取日志失败");
		}finally{
			json = JSONObject.fromObject(map);
		}
		return SUCCESS;
	}

	public JSON getJson() {
		return json;
	}

	public void setJson(JSON json) {
		this.json = json;
	}
}
